package com.forehope.portal;

import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.cc.RDSAPI;
import jodd.util.StringUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class testJob implements IJob {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        RDSAPI rds = SDK.getCCAPI().getRDSAPI("9b79c008-322d-401f-8350-b3685c072df6");
        String sql1 = "select DISTINCT BONAME from test_oracle";
        List<RowMap> list1 = rds.getMaps(sql1);
        String result = "";
        for (RowMap rowMap : list1) {
            String boName = rowMap.getString("BONAME");
            String sql = "select * from test_oracle where BONAME='" + boName + "'";
            List<RowMap> list = rds.getMaps(sql);
            /**
             * 组装建表语句
             */

            String sqlValueStart = "CREATE TABLE \"MIDUSER\".\"" + boName + "\"(";
            String other = "";

            String keySql = "ALTER TABLE \"MIDUSER\".\"" + boName + "\" ADD CONSTRAINT \"" + boName + "_PK\" PRIMARY KEY (";
            String resultName = "";
            for (RowMap rowMap1 : list) {
                String typeName = "";
                if (rowMap1.getString("TYPE").equals("CUKY") || rowMap1.getString("TYPE").equals("NUMC")) {
                    typeName = "NVARCHAR2(" + rowMap1.getString("LENGTH") + ")";
                } else if (rowMap1.getString("TYPE").equals("QUAN") || rowMap1.getString("TYPE").equals("CURR")) {
                    typeName = "DECIMAL(19,3)";
                } else if (rowMap1.getString("TYPE").equals("DEC")) {
                    typeName = "DECIMAL(19,0)";
                } else if (rowMap1.getString("TYPE").equals("INT2")) {
                    typeName = "NUMBER(" + rowMap1.getString("LENGTH") + ")";
                } else if (rowMap1.getString("TYPE").equals("CUKY")) {
                    typeName = "NVARCHAR2(" + rowMap1.getString("LENGTH") + ")";
                } else if (rowMap1.getString("TYPE").equals("DATS")) {
                    typeName = "NVARCHAR2(8)";
                } else if (rowMap1.getString("TYPE").equals("TIMS")) {
                    typeName = "NVARCHAR2(6)";
                } else {
                    typeName = (rowMap1.getString("TYPE").equals("CHAR")?"NVARCHAR2":rowMap1.getString("TYPE")) + "(" + rowMap1.getString("LENGTH") + " BYTE)";
                }
                other = other +  rowMap1.getString("FILENAME").toUpperCase()+" "+ typeName + ",";
                String iskey = rowMap1.getString("ISKEY");
                if (StringUtil.isNotEmpty(iskey)) {
                    resultName = resultName +  rowMap1.getString("FILENAME").toUpperCase() +  ",";
                }
            }
            resultName = resultName.substring(0, resultName.length() - 1);
            keySql = keySql + resultName + ");\n";
            other = other.substring(0, other.length() - 1);
            sqlValueStart = sqlValueStart + other + ");\n";
//            result = result+"======================================================================================================================\n";
            result = result + sqlValueStart;
            result = result + keySql + "\n\n";
//            result = result+"======================================================================================================================\n";
        }
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("sql.txt"))) {
            writer.write(result); // 写入文本
        } catch (IOException e) {
            e.printStackTrace(); // 处理异常
        }

    }
}
